<template>
  <section class="section hero is-fullheight is-error-section login-page">
    <div class="hero-body">
      <div class="container">
        <div class="columns is-centered">
          <div class="column is-two-fifths">
            <div class="card has-card-header-background">
              <header class="card-header">
                <p class="card-header-title"><span class="icon"><i class="mdi mdi-lock default"></i></span><span>Login</span></p>
              </header>
              <div class="card-content">
                <form @submit.prevent="handleLogin">
                  <b-field
                      label="账号" expanded>
                    <b-input
                        v-model="loginForm.username"
                        validation-message="请输入用户名"
                        placeholder="请输入用户名"
                        required/>
                  </b-field>
                  <b-field
                      label="密码"
                      expanded>
                    <b-input
                        v-model="loginForm.password"
                        type="password"
                        placeholder="请输入登录密码"
                        validation-message="请输入登录密码"
                        password-reveal required/>
                  </b-field>
                  <b-field>
                    <b-checkbox type="is-primary" v-model="loginForm.rememberMe">记住密码</b-checkbox>
                  </b-field>
                  <hr>
                  <b-field>
                    <b-field grouped>
                      <div class="control">
                        <b-button native-type="submit" type="is-primary" :loading="loading">登录</b-button>
                      </div>
                    </b-field>
                  </b-field>
                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="hero-foot has-text-centered">
      <div class="logo"></div>
    </div>
  </section>

</template>

<script>
import {title} from '@/config/settings'

export default {
  name: "Login",
  data() {
    return {
      title: title,
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "admin",
        password: "111111",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          {required: true, trigger: "blur", message: "用户名不能为空"}
        ],
        password: [
          {required: true, trigger: "blur", message: "密码不能为空"}
        ],
        code: [{required: true, trigger: "change", message: "验证码不能为空"}]
      },
      capsTooltip: false,
      loading: false,
      redirect: undefined,
      otherQuery: {}
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
  },
  methods: {
    checkCapslock(e) {
      const {key} = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    handleLogin() {
      this.loading = true
      this.$store.dispatch('user/login', this.loginForm)
          .then(() => {
            this.$router.push({path: this.redirect || '/', query: this.otherQuery})
            this.loading = false
          })
          .catch(() => {
            this.loading = false
          })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
};
</script>

<style lang="scss" scoped>

.section{
  padding: 0;
}
/*.login-page{
  margin-top: -4.25rem;
}
@include desktop{
  .login-page{
    margin-left: -13rem;
    margin-top: -4.25rem;
  }
}*/

.login-page {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  overflow: hidden;
  background-image: url("../../assets/images/login-background.jpg");
  background-size: cover;
  .card{
    background-color: rgba(255,255,255,0.7);
  }
}
</style>
